function init() {
  initMenu();
}

var initMenu = (function () {
  var oMenu = document.getElementsByClassName('menu-wrap')[0],
    oMenuItems = oMenu.getElementsByClassName('main-item'),
    oSub = oMenu.getElementsByClassName('sub')[0],
    oSubItems = oSub.getElementsByClassName('sub-item'),
    menuLen = oMenuItems.length,
    subLen = oSubItems.length,
    menuItem,
    subItem,
    isInSub = false,
    isFirst = true,
    t = null,
    // 鼠标坐标数组
    mousePoses = [];


  for (var i = 0; i < menuLen; i++) {
    menuItem = oMenuItems[i];
    addEvent(menuItem, 'mouseenter', menuItemMouseEnter);
  }

  addEvent(oMenu, 'mouseenter', function () {
    addEvent(document, 'mousemove', mouseMove);
  });
  addEvent(oMenu, 'mouseleave', menuMouseOut);

  //鼠标移入移出
  addEvent(oSub, 'mouseenter', function () {
    isInSub = true;
  })

  //鼠标移入移出
  addEvent(oSub, 'mouseleave', function () {
    isInSub = false;
  })

  function menuItemMouseEnter(e) {
    var e = e || window.event,
      tar = e.target || e.srcElement,
      thisIdx = Array.prototype.indexOf.call(oMenuItems, tar),
      lastPos = mousePoses[mousePoses.length - 2] || {
        x: 0,
        y: 0
      },
      curPos = mousePoses[mousePoses.length - 1] || {
        x: 0,
        y: 0
      },
      toDelay = doTimeout(lastPos, curPos);

    //显示sub盒子
    oSub.className = 'sub';

    if (t) {
      clearTimeout(t);
    }

    if (!isFirst) {
      if (toDelay) {
        t = setTimeout(function () {
          //鼠标在子菜单里
          if (isInSub) {
            return;
          }

          addActive(thisIdx);
          t = null;
        }, 300);
      } else {
        addActive(thisIdx);
      }
    } else {
      addActive(thisIdx);
      isFirst = false;
    }
  }

  //抽象：新增类名
  function addActive(index) {
    removeAllActive();
    oMenuItems[index].className += ' active';
    oSubItems[index].className += ' active';
  }

  //抽象：移除类名
  function removeAllActive(item) {
    for (var i = 0; i < menuLen; i++) {
      item = oMenuItems[i];
      item.className = 'main-item';
    }
    for (var i = 0; i < subLen; i++) {
      item = oSubItems[i];
      item.className = 'sub-item';
    }
  }

  function mouseMove(e) {
    var e = e || window.event;

    mousePoses.push({
      x: pagePos(e).X,
      y: pagePos(e).Y
    });

    if (mousePoses.length > 3) {
      //把第一个元素删除 把前面的删除
      mousePoses.shift();
    }
    console.log(mousePoses);
  }

  //鼠标移出菜单
  function menuMouseOut() {
    oSub.className += ' hide';
    removeAllActive();
    removeEvent(document, 'mousemove', mouseMove);
  }

  function doTimeout(lastPos, curPos) {
    var topLeft = {
      x: getStyles(oMenu, 'width') + getStyles(oMenu, 'margin-left'),
      y: getStyles(oMenu, 'margin-top')
    };
    var bottomLeft = {
      x: getStyles(oMenu, 'width') + getStyles(oMenu, 'margin-left'),
      y: getStyles(oMenu, 'margin-top') + getStyles(oSub, 'height')
    }
    return pointInTriangle(curPos, lastPos, topLeft, bottomLeft);
  }

});

init();